//
//  shopuserTextViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/11.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "shopuserTextViewController.h"

#import "AFNetworking.h"

#import "ReadLoginData.h"

<<<<<<< HEAD
@interface shopuserTextViewController ()
=======
#define WEIGHT [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface shopuserTextViewController () <UITableViewDataSource,UITableViewDelegate>
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee


@property (nonatomic, strong)NSMutableArray *shopusertextArray;

<<<<<<< HEAD
=======
@property (nonatomic, strong)UITableView *tableView;

>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
@end

@implementation shopuserTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self addshopusertextinitsth];
    
    [self addshopuserTexttopview];
    
   
<<<<<<< HEAD
    
=======
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WEIGHT, HEIGHT)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    self.view.backgroundColor = [UIColor orangeColor];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    
}

- (void)addshopusertextinitsth {
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请返回登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
<<<<<<< HEAD
=======
        
//        NSLog(@"%@",dic[@"data"]);
        
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        [self getuserDataSources:[NSString stringWithFormat:@"%@", [dic[@"data"] objectForKey:@"username"]]];
    }
    
    self.shopusertextArray = [@[]mutableCopy];
}

- (void)addshopuserTexttopview {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"合作记录";

}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)getuserDataSources:(NSString *)nameStr {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    
    NSString *username = nameStr;
  

    NSDictionary *param = @{ @"username":[NSString stringWithFormat:@"%@", username]};
    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Gethz" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        if ([[self toArrayOrNSDictionary:responseObject] isKindOfClass:[NSArray class]]) {
            self.shopusertextArray = [self toArrayOrNSDictionary:responseObject];
        }else {
        
        NSDictionary *dic = [self toArrayOrNSDictionary:responseObject];
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"%@", dic[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
         }
<<<<<<< HEAD
=======
        
        NSLog(@"%@",string);
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
 
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
//         NSLog(@"数据请求错误原因:%@", error);
//         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
